package com.luoyuanxiangvip.muisc.service.impl;

import com.luoyuanxiangvip.common.exception.CheckedException;
import com.luoyuanxiangvip.common.utils.RestClientUtil;
import com.luoyuanxiangvip.common.annotation.Cache;
import com.luoyuanxiangvip.muisc.service.IMuiscService;
import com.luoyuanxiangvip.muisc.utils.UrlBuildUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *
 * </p>
 *
 * @author luoyuanxiang <p>luoyuanxiangvip.com</p>
 * @since 2019/5/31
 */
@Service
@Slf4j
public class MuiscServiceImpl implements IMuiscService {

    /**
     * 获取歌曲列表
     *
     * @param playId 歌曲id
     * @return string
     */
    @Override
    @Cache(value = "获取歌单信息",expire = 24, unit = TimeUnit.HOURS)
    public String getPlaylist(String playId){
        if(StringUtils.isEmpty(playId)) {
            throw new CheckedException("歌单id为空");
        }
        return RestClientUtil.get(UrlBuildUtils.getWangyiMusicPlayListUrl(playId), "UTF-8");
    }

    /**
     * 获取歌曲歌词
     *
     * @param musicId 歌曲id
     * @return string
     */
    @Override
    @Cache(value = "获取歌曲歌词",expire = 30, unit = TimeUnit.MINUTES)
    public String getLyric(String musicId){
        if(StringUtils.isEmpty(musicId)) {
            throw new CheckedException("歌曲id为空");
        }
        return RestClientUtil.get(UrlBuildUtils.getWangyiLrcUrl(musicId), "UTF-8");
    }

    /**
     * 获取网易云音乐歌曲的链接
     *
     * @param musicId 歌曲id
     * @return string
     */
    @Override
    @Cache(value = "获取网易云音乐歌曲的链接",expire = 30, unit = TimeUnit.MINUTES)
    public String getSongUrl(String musicId){
        if(StringUtils.isEmpty(musicId)) {
            throw new CheckedException("歌曲id为空");
        }
        return RestClientUtil.get(UrlBuildUtils.getWangyiSongUrl(musicId), "UTF-8");
    }

    /**
     * 获取歌曲信息
     *
     * @param musicId 歌曲id
     * @return string
     */
    @Override
    @Cache(value = "获取歌曲信息",expire = 30, unit = TimeUnit.MINUTES)
    public String getSongDetail(String musicId){
        if(StringUtils.isEmpty(musicId)) {
            throw new CheckedException("歌曲id为空");
        }
        return RestClientUtil.get(UrlBuildUtils.getGetWangyiSongDetail(musicId), "UTF-8");
    }
}
